# delimit ;  
set more 1 ;  
set mem 500m;
* set this to 1 if include 2020 wave15;
global INCLUDE2020 0;
local UNIX 0;


*global folder "C:\research";
*global folder "C:\Users\ali_merali\Downloads";
*global folder "\\econ-san-fs\Home4\uctpkp0\SystemFolder\Windows7\Desktop";
*global folder "\\ad.ucl.ac.uk\homer\zctpmer\downloads";
global folder "D:\Dropbox\";

*global folder "C:\Users\Jeremy\Dropbox";
*global folder "~/";

         local saved  "$folder\hrs\merge\";
         local trackd "$folder\hrs\tracker\";
         local wave1d "$folder\hrs\wave1\";
         local wave2d "$folder\hrs\wave2\";
         local wave3d "$folder\hrs\wave3\";
         local wave4d "$folder\hrs\wave4\";
         local wave5d "$folder\hrs\wave5\";
		 local wave6d "$folder\hrs\wave6\";
         local wave7d "$folder\hrs\wave7\";
         local wave8d "$folder\hrs\wave8\";
		 local wave9d "$folder\hrs\wave9\";
		 local wave10d "$folder\hrs\wave10\";
		 local wave11d "$folder\hrs\wave11\";
		 local wave12d "$folder\hrs\wave12\";
		 local wave13d "$folder\hrs\wave13\";
		 local wave14d "$folder\hrs\wave14\";
		 local wave15d "$folder\hrs\wave15\";
         local aheadw1 "$folder\hrs\aheadw1\";
         local aheadw2 "$folder\hrs\aheadw2\";
*log using `saved'merge2.log, replace ;

use `saved'merge1;

gen switch1=.;
gen switch2=.;
gen switch3=.;
gen switch4=.;
gen switch5=.;
gen switch6=.;
gen switch7=.;
gen switch8=.;
gen switch9=.;
gen switch10=.;
gen switch11=.;
gen switch12=.;
gen switch13=.;
gen switch14=.;
if $INCLUDE2020 {;
gen switch15=.;
};

gen socc1=.;
gen socc2=.;
gen socc3=.;
gen socc4=.;
gen socc5=.;
gen socc6=.;
gen socc7=.;
gen socc8=.;
gen socc9=.;
gen socc10=.;
gen socc11=.;
gen socc12=.;
gen socc13=.;
gen socc14=.;
if $INCLUDE2020 {;
gen socc15=.;
};
summ birthyr;

*age when people started working for current employer;
replace switch1=tenure1-birthyr if tenure1~=. & birthyr~=.;
replace switch2=tenure2-birthyr if tenure2~=. & birthyr~=.;
replace switch3=tenure3-birthyr if tenure3~=. & birthyr~=.;
replace switch4=tenure4-birthyr if tenure4~=. & birthyr~=.;
replace switch5=tenure5-birthyr if tenure5~=. & birthyr~=.;
replace switch6=tenure6-birthyr if tenure6~=. & birthyr~=.;
replace switch7=tenure7-birthyr if tenure7~=. & birthyr~=.;
replace switch8=tenure8-birthyr if tenure8~=. & birthyr~=.;
replace switch9=tenure9-birthyr if tenure9~=. & birthyr~=.;
replace switch10=tenure10-birthyr if tenure10~=. & birthyr~=.;
replace switch11=tenure11-birthyr if tenure11~=. & birthyr~=.;
replace switch12=tenure12-birthyr if tenure12~=. & birthyr~=.;
replace switch13=tenure13-birthyr if tenure13~=. & birthyr~=.;
replace switch14=tenure14-birthyr if tenure14~=. & birthyr~=.;
if $INCLUDE2020 {;
replace switch15=tenure15-birthyr if tenure15~=. & birthyr~=.;
};
* if not working, switch=0.  Not sure how to deal with people working 1 to 300 hours per year;
replace switch1=. if hours1==0|hours1==.;
replace switch2=. if hours2==0|hours2==.;
replace switch3=. if hours3==0|hours3==.;
replace switch4=. if hours4==0|hours4==.;
replace switch5=. if hours5==0|hours5==.;
replace switch6=. if hours6==0|hours6==.;
replace switch7=. if hours7==0|hours7==.;
replace switch8=. if hours8==0|hours8==.;
replace switch9=. if hours9==0|hours9==.;
replace switch10=. if hours10==0|hours10==.;
replace switch11=. if hours11==0|hours11==.;
replace switch12=. if hours12==0|hours12==.;
replace switch13=. if hours13==0|hours13==.;
replace switch14=. if hours14==0|hours14==.;
if $INCLUDE2020 {;
replace switch15=. if hours15==0|hours15==.;
};

*age when people started working in current occupation;
replace socc1=job1-birthyr if job1~=. & birthyr~=.;
replace socc2=job2-birthyr if job2~=. & birthyr~=.;
replace socc3=job3-birthyr if job3~=. & birthyr~=.;
replace socc4=job4-birthyr if job4~=. & birthyr~=.;
replace socc5=job5-birthyr if job5~=. & birthyr~=.;
replace socc6=job6-birthyr if job6~=. & birthyr~=.;
replace socc7=job7-birthyr if job7~=. & birthyr~=.;
replace socc8=job8-birthyr if job8~=. & birthyr~=.;
replace socc9=job9-birthyr if job9~=. & birthyr~=.;
replace socc10=job10-birthyr if job10~=. & birthyr~=.;
replace socc11=job11-birthyr if job11~=. & birthyr~=.;
replace socc12=job12-birthyr if job12~=. & birthyr~=.;
replace socc13=job13-birthyr if job13~=. & birthyr~=.;
replace socc14=job14-birthyr if job14~=. & birthyr~=.;
if $INCLUDE2020 {;
replace socc15=job15-birthyr if job15~=. & birthyr~=.;
};

*number of years working for current employer-this is not exactly correct for AHEAD waves 1 and 2;
if $INCLUDE2020 {;
replace tenure15=2020-tenure15;
};
replace tenure14=2018-tenure14;
replace tenure13=2016-tenure13;
replace tenure12=2014-tenure12;
replace tenure11=2012-tenure11;
replace tenure10=2010-tenure10;
replace tenure9=2008-tenure9;
replace tenure8=2006-tenure8;
replace tenure7=2004-tenure7;
replace tenure6=2002-tenure6;
replace tenure5=2000-tenure5;
replace tenure4=1998-tenure4;
replace tenure3=1996-tenure3;
replace tenure2=1994-tenure2;
replace tenure1=1992-tenure1;

sum ten*;

*tenure imputations ;
*replace tenure2=(tenure1+2) if tenure2==. & same2==1;
replace tenure3=(tenure2+2) if tenure3==. & same3==1;
replace tenure4=(tenure3+2) if tenure4==. & same4==1;
replace tenure5=(tenure4+2) if tenure5==. & same5==1;
replace tenure6=(tenure5+2) if tenure6==. & same6==1;
replace tenure7=(tenure6+2) if tenure7==. & same7==1;
replace tenure8=(tenure7+2) if tenure8==. & same8==1;
replace tenure9=(tenure8+2) if tenure9==. & same9==1;
replace tenure10=(tenure9+2) if tenure10==. & same10==1;
replace tenure11=(tenure10+2) if tenure11==. & same11==1;
replace tenure12=(tenure11+2) if tenure12==. & same12==1;
replace tenure13=(tenure12+2) if tenure13==. & same13==1;
replace tenure14=(tenure13+2) if tenure14==. & same14==1;
if $INCLUDE2020 {;
replace tenure15=(tenure14+2) if tenure15==. & same15==1;
};
sum ten*;

replace tenure1=0 if tenure1==. | tenure1<0;
replace tenure2=0 if tenure2==. | tenure2<0;
replace tenure3=0 if tenure3==.;
replace tenure4=0 if tenure4==.;
replace tenure5=0 if tenure5==.;
replace tenure6=0 if tenure6==.;
replace tenure7=0 if tenure7==.;
replace tenure8=0 if tenure8==.;
replace tenure9=0 if tenure9==.;
replace tenure10=0 if tenure9==.;
replace tenure11=0 if tenure10==.;
replace tenure12=0 if tenure11==.;
replace tenure13=0 if tenure12==.;
replace tenure14=0 if tenure12==.;
if $INCLUDE2020 {;
replace tenure15=0 if tenure12==.;
};
sum ten*;

* if not working, tenure=0.  Not sure how to deal with people working 1 to 300 hours per year;
replace tenure1=0 if hours1==0|hours1==.;
replace tenure2=0 if hours2==0|hours2==.;
replace tenure3=0 if hours3==0|hours3==.;
replace tenure4=0 if hours4==0|hours4==.;
replace tenure5=0 if hours5==0|hours5==.;
replace tenure6=0 if hours6==0|hours6==.;
replace tenure7=0 if hours7==0|hours7==.;
replace tenure8=0 if hours8==0|hours8==.;
replace tenure9=0 if hours9==0|hours9==.;
replace tenure10=0 if hours9==0|hours9==.;
replace tenure11=0 if hours10==0|hours10==.;
replace tenure12=0 if hours11==0|hours11==.;
replace tenure13=0 if hours12==0|hours12==.;
replace tenure14=0 if hours13==0|hours13==.;
if $INCLUDE2020 {;
replace tenure15=0 if hours14==0|hours14==.;
};
*number of years working in current occupation;
if $INCLUDE2020 {;
replace job15=2020-job15;
};
replace job14=2018-job14;
replace job13=2016-job13;
replace job12=2014-job12;
replace job11=2012-job11;
replace job10=2010-job10;
replace job9=2008-job9;
replace job8=2006-job8;
replace job7=2004-job7;
replace job6=2002-job6;
replace job5=2000-job5;
replace job4=1998-job4;
replace job3=1996-job3;
replace job2=1994-job2;
replace job1=1992-job1;
replace job1=0 if job1<0; //to account for different years of the AHEAD waves;
replace job2=0 if job2<0;

* merge in dead with alive people;
* do medical costs first;
replace oop2=doop2 if dead2==1 & oop2==.;
replace oop3=doop3 if dead3==1 & oop3==.;
replace oop4=doop4 if dead4==1 & oop4==.;
replace oop5=doop5 if dead5==1 & oop5==.;
replace oop6=doop6 if doop6~=. & oop6==.;
replace oop7=doop7 if doop7~=. & oop7==.;
replace oop8=doop8 if doop8~=. & oop8==.;
replace oop9=doop9 if doop9~=. & oop9==.;
replace oop10=doop10 if doop10~=. & oop10==.;
replace oop11=doop11 if doop11~=. & oop11==.;
replace oop12=doop12 if doop12~=. & oop12==.;
replace oop13=doop13 if doop13~=. & oop13==.;
replace oop14=doop14 if doop14~=. & oop14==.;
if $INCLUDE2020 {;
replace oop15=doop15 if doop15~=. & oop15==.;
};

replace drugc2=ddrugc2 if dead2==1 & drugc2==.;
replace drugc3=ddrugc3 if dead3==1 & drugc3==.;
replace drugc4=ddrugc4 if dead4==1 & drugc4==.;
replace drugc5=ddrugc5 if dead5==1 & drugc5==.;
replace drugc6=ddrugc6 if ddrugc6~=. & drugc6==.;
replace drugc7=ddrugc7 if ddrugc7~=. & drugc7==.;
replace drugc8=ddrugc8 if ddrugc8~=. & drugc8==.;
replace drugc9=ddrugc9 if ddrugc9~=. & drugc9==.;
replace drugc10=ddrugc10 if ddrugc10~=. & drugc10==.;
replace drugc11=ddrugc11 if ddrugc11~=. & drugc11==.;
replace drugc12=ddrugc12 if ddrugc12~=. & drugc12==.;
replace drugc13=ddrugc13 if ddrugc13~=. & drugc13==.;
replace drugc14=ddrugc14 if ddrugc14~=. & drugc14==.;
if $INCLUDE2020 {;
replace drugc15=ddrugc15 if ddrugc15~=. & drugc15==.;
};
replace iprem2=diprem2 if dead2==1 & iprem2==.;
replace iprem3=diprem3 if dead3==1 & iprem3==.;
replace iprem4=diprem4 if dead4==1 & iprem4==.;
replace iprem5=diprem5 if dead5==1 & iprem5==.;
replace iprem6=diprem6 if diprem6~=. & iprem6==.;
replace iprem7=diprem7 if diprem7~=. & iprem7==.;
replace iprem8=diprem8 if diprem8~=. & iprem8==.;
replace iprem9=diprem9 if diprem9~=. & iprem9==.;
replace iprem10=diprem10 if diprem10~=. & iprem10==.;
replace iprem11=diprem11 if diprem11~=. & iprem11==.;
replace iprem12=diprem12 if diprem12~=. & iprem12==.;
replace iprem13=diprem13 if diprem13~=. & iprem13==.;
replace iprem14=diprem14 if diprem14~=. & iprem14==.;
if $INCLUDE2020 {;
replace iprem15=diprem15 if diprem15~=. & iprem15==.;
};

replace medcost2= dmedc2 if dead2==1 & medcost2==.;
replace medcost3= dmedc3 if dead3==1 & medcost3==.;
replace medcost4= dmedc4 if dead4==1 & medcost4==.;
replace medcost5= dmedc5 if dead5==1 & medcost5==.;
replace medcost6= dmedc6 if dmedc6~=. & medcost6==.;
replace medcost7= dmedc7 if dmedc7~=. & medcost7==.;
replace medcost8= dmedc8 if dmedc8~=. & medcost8==.;
replace medcost9= dmedc9 if dmedc9~=. & medcost9==.;
replace medcost10= dmedc10 if dmedc10~=. & medcost10==.;
replace medcost11= dmedc11 if dmedc11~=. & medcost11==.;
replace medcost12= dmedc12 if dmedc12~=. & medcost12==.;
replace medcost13= dmedc13 if dmedc13~=. & medcost13==.;
replace medcost14= dmedc14 if dmedc14~=. & medcost14==.;
if $INCLUDE2020 {;
replace medcost15= dmedc15 if dmedc15~=. & medcost15==.;
};
replace nursing2= dnursing2 if dead2==1 & nursing2==.;
replace nursing3= dnursing3 if dead3==1 & nursing3==.;
replace nursing4= dnursing4 if dead4==1 & nursing4==.;
replace nursing5= dnursing5 if dead5==1 & nursing5==.;
replace nursing6= dnursing6 if dnursing6~=. & nursing6==.;
replace nursing7= dnursing7 if dnursing7~=. & nursing7==.;
replace nursing8= dnursing8 if dnursing8~=. & nursing8==.;
replace nursing9= dnursing9 if dnursing9~=. & nursing9==.;
replace nursing10= dnursing10 if dnursing10~=. & nursing10==.;
replace nursing11= dnursing11 if dnursing11~=. & nursing11==.;
replace nursing12= dnursing12 if dnursing12~=. & nursing12==.;
replace nursing13= dnursing13 if dnursing13~=. & nursing13==.;
replace nursing14= dnursing14 if dnursing14~=. & nursing14==.;
if $INCLUDE2020 {;
replace nursing15= dnursing15 if dnursing15~=. & nursing15==.;
};
replace tc2=dtc2 if dead2==1 & tc2==.;
replace tc3=dtc3 if dead3==1 & tc3==.;
replace tc4=dtc4 if dead4==1 & tc4==.;
replace tc5=. if dead5==1 ; * this is an ugly fix right here;
sum *tc5;
replace tc5=dtc5 if dead5==1 & tc5==.;
replace tc6=dtc6 if dead6==1 & tc6==.;

* Handle the new variables ;

* hospc starts in 2 and dhospc starts in 6;
replace hospc6=dhospc6 if dhospc6~=. & hospc6==.;
replace hospc7=dhospc7 if dhospc7~=. & hospc7==.;
replace hospc8=dhospc8 if dhospc8~=. & hospc8==.;
replace hospc9=dhospc9 if dhospc9~=. & hospc9==.;
replace hospc10=dhospc10 if dhospc10~=. & hospc10==.;
replace hospc11=dhospc11 if dhospc11~=. & hospc11==.;
replace hospc12=dhospc12 if dhospc12~=. & hospc12==.;
replace hospc13=dhospc13 if dhospc13~=. & hospc13==.;
replace hospc14=dhospc14 if dhospc14~=. & hospc14==.;
if $INCLUDE2020 {;
replace hospc15=dhospc15 if dhospc15~=. & hospc15==.;
};
* nursc and dnursc start in 6;
replace nursc6=dnursc6 if dnursc6~=. & nursc6==.;
replace nursc7=dnursc7 if dnursc7~=. & nursc7==.;
replace nursc8=dnursc8 if dnursc8~=. & nursc8==.;
replace nursc9=dnursc9 if dnursc9~=. & nursc9==.;
replace nursc10=dnursc10 if dnursc10~=. & nursc10==.;
replace nursc11=dnursc11 if dnursc11~=. & nursc11==.;
replace nursc12=dnursc12 if dnursc12~=. & nursc12==.;
replace nursc13=dnursc13 if dnursc13~=. & nursc13==.;
replace nursc14=dnursc14 if dnursc14~=. & nursc14==.;
if $INCLUDE2020 {;
replace nursc15=dnursc15 if dnursc15~=. & nursc15==.;
* outdgc starts in 6 and has no version for the dead;
};
* docc starts in 2 and ddocc starts in 6;
replace docc6=ddocc6 if ddocc6~=. & docc6==.;
replace docc7=ddocc7 if ddocc7~=. & docc7==.;
replace docc8=ddocc8 if ddocc8~=. & docc8==.;
replace docc9=ddocc9 if ddocc9~=. & docc9==.;
replace docc10=ddocc10 if ddocc10~=. & docc10==.;
replace docc11=ddocc11 if ddocc11~=. & docc11==.;
replace docc12=ddocc12 if ddocc12~=. & docc12==.;
replace docc13=ddocc13 if ddocc13~=. & docc13==.;
replace docc14=ddocc14 if ddocc14~=. & docc14==.;
if $INCLUDE2020 {;
replace docc15=ddocc15 if ddocc15~=. & docc15==.;
};
* hhsc and dhhsc start in 6;
replace hhsc6=dhhsc6 if dhhsc6~=. & hhsc6==.;
replace hhsc7=dhhsc7 if dhhsc7~=. & hhsc7==.;
replace hhsc8=dhhsc8 if dhhsc8~=. & hhsc8==.;
replace hhsc9=dhhsc9 if dhhsc9~=. & hhsc9==.;
replace hhsc10=dhhsc10 if dhhsc10~=. & hhsc10==.;
replace hhsc11=dhhsc11 if dhhsc11~=. & hhsc11==.;
replace hhsc12=dhhsc12 if dhhsc12~=. & hhsc12==.;
replace hhsc13=dhhsc13 if dhhsc13~=. & hhsc13==.;
replace hhsc14=dhhsc14 if dhhsc14~=. & hhsc14==.;
if $INCLUDE2020 {;
replace hhsc15=dhhsc15 if dhhsc15~=. & hhsc15==.;
};
* othels starts in 2, dothels starts in 6;
replace othels6=dothels6 if dothels6~=. & othels6==.;
replace othels7=dothels7 if dothels7~=. & othels7==.;
replace othels8=dothels8 if dothels8~=. & othels8==.;
replace othels9=dothels9 if dothels9~=. & othels9==.;
replace othels10=dothels10 if dothels10~=. & othels10==.;
replace othels11=dothels11 if dothels11~=. & othels11==.;
replace othels12=dothels12 if dothels12~=. & othels12==.;
replace othels13=dothels13 if dothels13~=. & othels13==.;
replace othels14=dothels14 if dothels14~=. & othels14==.;
if $INCLUDE2020 {;
replace othels15=dothels15 if dothels15~=. & othels15==.;
};
* setting dental costs for dead people to zero since ddentc is missing;
replace dentc6 = 0 if dentc6 ==. & dhhsc6~=. ;
replace dentc7 = 0 if dentc7 ==. & dhhsc7~=. ;
replace dentc8 = 0 if dentc8 ==. & dhhsc8~=. ;
replace dentc9 = 0 if dentc9 ==. & dhhsc9~=. ;
replace dentc10 = 0 if dentc10 ==. & dhhsc10~=. ;
replace dentc11 = 0 if dentc11 ==. & dhhsc11~=. ;
replace dentc12 = 0 if dentc12 ==. & dhhsc12~=. ;
replace dentc13 = 0 if dentc13 ==. & dhhsc13~=. ;
replace dentc14 = 0 if dentc14 ==. & dhhsc14~=. ;
if $INCLUDE2020 {;
replace dentc15 = 0 if dentc15 ==. & dhhsc15~=. ;
};
* setting dental costs for dead people to zero since ddentc is missing;
replace outsgc6 = 0 if outsgc6 ==. & dhhsc6~=. ;
replace outsgc7 = 0 if outsgc7 ==. & dhhsc7~=. ;
replace outsgc8 = 0 if outsgc8 ==. & dhhsc8~=. ;
replace outsgc9 = 0 if outsgc9 ==. & dhhsc9~=. ;
replace outsgc10 = 0 if outsgc10 ==. & dhhsc10~=. ;
replace outsgc11 = 0 if outsgc11 ==. & dhhsc11~=. ;
replace outsgc12 = 0 if outsgc12 ==. & dhhsc12~=. ;
replace outsgc13 = 0 if outsgc13 ==. & dhhsc13~=. ;
replace outsgc14 = 0 if outsgc14 ==. & dhhsc14~=. ;
if $INCLUDE2020 {;
replace outsgc15 = 0 if outsgc15 ==. & dhhsc15~=. ;
};
* now do estates;
* no observations for wave 3;
/* gen houseestate3=house2;
sort housebequeathA3;
by housebequeathA3: sum assets2 estate3 houseestate3;
by housebequeathA3: reg assets2 estate3 houseestate3;
*/

gen houseestate4=house3;
sort housebequeathA4;
by housebequeathA4: sum assets3 estate4 houseestate4 if (assets3<1000000 & estate4<1000000);
by housebequeathA4: reg assets3 estate4 houseestate4 if (assets3<1000000 & estate4<1000000);

gen houseestate5=house4;
sort housebequeathA5;
by housebequeathA5: sum assets4 estate5 houseestate5 if (assets4<1000000 & estate5<1000000);
by housebequeathA5: reg assets4 estate5 houseestate5 if (assets4<1000000 & estate5<1000000);

gen houseestate6=house5;
sort housebequeathA6;
by housebequeathA6: sum assets5 estate6 houseestate6 if (assets5<1000000 & estate6<1000000);
by housebequeathA6: reg assets5 estate6 houseestate6 if (assets5<1000000 & estate6<1000000);

gen houseestate7=house6;
sort housebequeathA7;
by housebequeathA7: sum assets6 estate7 houseestate7 if (assets6<1000000 & estate7<1000000);
by housebequeathA7: reg assets6 estate7 houseestate7 if (assets6<1000000 & estate7<1000000);

gen houseestate8=house7;
sort housebequeathA8;
by housebequeathA8: sum assets7 estate8 houseestate8 if (assets7<1000000 & estate8<1000000);
by housebequeathA8: reg assets7 estate8 houseestate8 if (assets7<1000000 & estate8<1000000);

gen houseestate9=house8;
sort housebequeathA9;
by housebequeathA9: sum assets8 estate9 houseestate9 if (assets8<1000000 & estate9<1000000);
by housebequeathA9: reg assets8 estate9 houseestate9 if (assets8<1000000 & estate9<1000000);

gen houseestate10=house9;
sort housebequeathA10;
by housebequeathA10: sum assets9 estate10 houseestate10 if (assets9<1000000 & estate10<1000000);
by housebequeathA10: reg assets9 estate10 houseestate10 if (assets9<1000000 & estate10<1000000);


gen houseestate11=house10;
sort housebequeathA11;
by housebequeathA11: sum assets10 estate11 houseestate11 if (assets10<1000000 & estate11<1000000);
by housebequeathA11: reg assets10 estate11 houseestate11 if (assets10<1000000 & estate11<1000000);

gen houseestate12=house11;
sort housebequeathA12;
by housebequeathA12: sum assets11 estate12 houseestate12 if (assets11<1000000 & estate12<1000000);
by housebequeathA12: reg assets11 estate12 houseestate12 if (assets11<1000000 & estate12<1000000);

gen houseestate13=house12;
sort housebequeathA13;
by housebequeathA13: sum assets12 estate13 houseestate13 if (assets12<1000000 & estate13<1000000);
by housebequeathA13: reg assets12 estate13 houseestate13 if (assets12<1000000 & estate13<1000000);


gen houseestate14=house13;
sort housebequeathA14;
by housebequeathA14: sum assets13 estate14 houseestate14 if (assets13<1000000 & estate14<1000000);
by housebequeathA14: reg assets13 estate14 houseestate14 if (assets13<1000000 & estate14<1000000);

if $INCLUDE2020 {;
gen houseestate15=house14;
sort housebequeathA15;
by housebequeathA15: sum assets14 estate15 houseestate15 if (assets14<1000000 & estate15<1000000);
by housebequeathA15: reg assets14 estate15 houseestate15 if (assets14<1000000 & estate15<1000000);
};

*****************************;
* age is sometimes screwed up;
* replace age with realage   ;
*****************************;
gen rage1=int(realage1);
gen rage2=int(realage2);
gen rage3=int(realage3);
gen rage4=int(realage4);
gen rage5=int(realage5);
gen rage6=int(realage6);
gen rage7=int(realage7);
gen rage8=int(realage8);
gen rage9=int(realage9);
gen rage10=int(realage10);
gen rage11=int(realage11);
gen rage12=int(realage12);
gen rage13=int(realage13);
gen rage14=int(realage14);
if $INCLUDE2020 {;
gen rage15=int(realage15);
};

replace age1=rage1 if rage1~=.;
replace age2=rage2 if rage2~=.;
replace age3=rage3 if rage3~=.;
replace age4=rage4 if rage4~=.;
replace age5=rage5 if rage5~=.;
replace age6=rage6 if rage6~=.;
replace age7=rage7 if rage7~=.;
replace age8=rage8 if rage8~=.;
replace age9=rage9 if rage9~=.;
replace age10=rage10 if rage10~=.;
replace age11=rage11 if rage11~=.;
replace age12=rage12 if rage12~=.;
replace age13=rage13 if rage13~=.;
replace age14=rage14 if rage14~=.;
if $INCLUDE2020 {;
replace age15=rage15 if rage15~=.;
};

sum age* rage*;
drop rage*;
* get ages of dead people;
* note: there are still a few dead people for whom;
* I do not have their age.  all these people ;
* get dropped in merge4 ;
replace age2=age1+2 if dead2==1 & age2==.;

replace age3=age2+2 if dead3==1 & age3==.;
replace age3=age1+4 if dead3==1 & age3==.;

replace age4=age3+2 if dead4==1 & age4==.;
replace age4=age2+4 if dead4==1 & age4==.;
replace age4=age1+6 if dead4==1 & age4==.;

replace age5=age4+2 if dead5==1 & age5==.;
replace age5=age3+4 if dead5==1 & age5==.;
replace age5=age2+6 if dead5==1 & age5==.;
replace age5=age1+8 if dead5==1 & age5==.;

replace age6=age5+2 if dead6==1 & age6==.;
replace age6=age4+4 if dead6==1 & age6==.;
replace age6=age3+6 if dead6==1 & age6==.;
replace age6=age2+8 if dead6==1 & age6==.;
replace age6=age1+10 if dead6==1 & age6==.;

replace age7=age6+2 if dead7==1 & age7==.;
replace age7=age5+4 if dead7==1 & age7==.;
replace age7=age4+6 if dead7==1 & age7==.;
replace age7=age3+8 if dead7==1 & age7==.;
replace age7=age2+10 if dead7==1 & age7==.;
replace age7=age1+12 if dead7==1 & age7==.;

replace age8=age7+2 if dead8==1 & age8==.;
replace age8=age6+4 if dead8==1 & age8==.;
replace age8=age5+6 if dead8==1 & age8==.;
replace age8=age4+8 if dead8==1 & age8==.;
replace age8=age3+10 if dead8==1 & age8==.;
replace age8=age2+12 if dead8==1 & age8==.;
replace age8=age1+14 if dead8==1 & age8==.;

replace age9=age8+2 if dead9==1 & age9==.;
replace age9=age7+4 if dead9==1 & age9==.;
replace age9=age6+6 if dead9==1 & age9==.;
replace age9=age5+8 if dead9==1 & age9==.;
replace age9=age4+10 if dead9==1 & age9==.;
replace age9=age3+12 if dead9==1 & age9==.;
replace age9=age2+14 if dead9==1 & age9==.;
replace age9=age1+16 if dead9==1 & age9==.;

replace age10=age9+2 if dead10==1 & age10==.;
replace age10=age8+4 if dead10==1 & age10==.;
replace age10=age7+6 if dead10==1 & age10==.;
replace age10=age6+8 if dead10==1 & age10==.;
replace age10=age5+10 if dead10==1 & age10==.;
replace age10=age4+12 if dead10==1 & age10==.;
replace age10=age3+14 if dead10==1 & age10==.;
replace age10=age2+16 if dead10==1 & age10==.;
replace age10=age1+18 if dead10==1 & age10==.;

replace age11=age10+2 if dead11==1 & age11==.;
replace age11=age9+4 if dead11==1 & age11==.;
replace age11=age8+6 if dead11==1 & age11==.;
replace age11=age7+8 if dead11==1 & age11==.;
replace age11=age6+10 if dead11==1 & age11==.;
replace age11=age5+12 if dead11==1 & age11==.;
replace age11=age4+14 if dead11==1 & age11==.;
replace age11=age3+16 if dead11==1 & age11==.;
replace age11=age2+18 if dead11==1 & age11==.;
replace age11=age1+20 if dead11==1 & age11==.;

replace age12=age11+2 if dead12==1 & age12==.;
replace age12=age10+4 if dead12==1 & age12==.;
replace age12=age9+6 if dead12==1 & age12==.;
replace age12=age8+8 if dead12==1 & age12==.;
replace age12=age7+10 if dead12==1 & age12==.;
replace age12=age6+12 if dead12==1 & age12==.;
replace age12=age5+14 if dead12==1 & age12==.;
replace age12=age4+16 if dead12==1 & age12==.;
replace age12=age3+18 if dead12==1 & age12==.;
replace age12=age2+20 if dead12==1 & age12==.;
replace age12=age1+22 if dead12==1 & age12==.;

replace age13=age12+2 if dead13==1 & age13==.;
replace age13=age11+4 if dead13==1 & age13==.;
replace age13=age10+6 if dead13==1 & age13==.;
replace age13=age9+8 if dead13==1 & age13==.;
replace age13=age8+10 if dead13==1 & age13==.;
replace age13=age7+12 if dead13==1 & age13==.;
replace age13=age6+14 if dead13==1 & age13==.;
replace age13=age5+16 if dead13==1 & age13==.;
replace age13=age4+18 if dead13==1 & age13==.;
replace age13=age3+20 if dead13==1 & age13==.;
replace age13=age2+22 if dead13==1 & age13==.;
replace age13=age1+24 if dead13==1 & age13==.;

replace age14=age13+2 if dead14==1 & age14==.;
replace age14=age12+4 if dead14==1 & age14==.;
replace age14=age11+6 if dead14==1 & age14==.;
replace age14=age10+8 if dead14==1 & age14==.;
replace age14=age9+10 if dead14==1 & age14==.;
replace age14=age8+12 if dead14==1 & age14==.;
replace age14=age7+14 if dead14==1 & age14==.;
replace age14=age6+16 if dead14==1 & age14==.;
replace age14=age5+18 if dead14==1 & age14==.;
replace age14=age4+20 if dead14==1 & age14==.;
replace age14=age3+22 if dead14==1 & age14==.;
replace age14=age2+24 if dead14==1 & age14==.;
replace age14=age1+26 if dead14==1 & age14==.;

if $INCLUDE2020 {;
replace age15=age14+2 if dead15==1 & age15==.;
replace age15=age13+4 if dead15==1 & age15==.;
replace age15=age12+6 if dead15==1 & age15==.;
replace age15=age11+8 if dead15==1 & age15==.;
replace age15=age10+10 if dead15==1 & age15==.;
replace age15=age9+12 if dead15==1 & age15==.;
replace age15=age8+14 if dead15==1 & age15==.;
replace age15=age7+16 if dead15==1 & age15==.;
replace age15=age6+18 if dead15==1 & age15==.;
replace age15=age5+20 if dead15==1 & age15==.;
replace age15=age4+22 if dead15==1 & age15==.;
replace age15=age3+24 if dead15==1 & age15==.;
replace age15=age2+26 if dead15==1 & age15==.;
replace age15=age1+28 if dead15==1 & age15==.;
};


replace realage2=realage1+2 if dead2==1 & realage2==.;
replace realage3=realage2+2 if dead3==1 & realage3==.;
replace realage4=realage3+2 if dead4==1 & realage4==.;
replace realage5=realage4+2 if dead5==1 & realage5==.;
replace realage6=realage5+2 if dead6==1 & realage6==.;
replace realage7=realage6+2 if dead7==1 & realage7==.;
replace realage8=realage7+2 if dead8==1 & realage8==.;
replace realage9=realage8+2 if dead9==1 & realage9==.;
replace realage10=realage9+2 if dead10==1 & realage10==.;
replace realage11=realage10+2 if dead11==1 & realage11==.;
replace realage12=realage11+2 if dead12==1 & realage12==.;
replace realage13=realage12+2 if dead13==1 & realage13==.;
replace realage14=realage12+2 if dead14==1 & realage14==.;
if $INCLUDE2020 {;
replace realage15=realage12+2 if dead15==1 & realage15==.;
};

replace married2=married1 if dead2==1 & married2==.;
replace married3=married2 if dead3==1 & married3==.;
replace married3=married1 if dead3==1 & married3==.;

replace married4=married3 if dead4==1 & married4==.;
replace married4=married2 if dead4==1 & married4==.;
replace married4=married1 if dead4==1 & married4==.;

replace married5=married4 if dead5==1 & married5==.;
replace married5=married3 if dead5==1 & married5==.;
replace married5=married2 if dead5==1 & married5==.;
replace married5=married1 if dead5==1 & married5==.;

replace married6=married5 if dead6==1 & married6==.;
replace married6=married4 if dead6==1 & married6==.;
replace married6=married3 if dead6==1 & married6==.;
replace married6=married2 if dead6==1 & married6==.;
replace married6=married1 if dead6==1 & married6==.;

replace married7=married6 if dead7==1 & married7==.;
replace married7=married5 if dead7==1 & married7==.;
replace married7=married4 if dead7==1 & married7==.;
replace married7=married3 if dead7==1 & married7==.;
replace married7=married2 if dead7==1 & married7==.;
replace married7=married1 if dead7==1 & married7==.;

replace married8=married7 if dead8==1 & married8==.;
replace married8=married6 if dead8==1 & married8==.;
replace married8=married5 if dead8==1 & married8==.;
replace married8=married4 if dead8==1 & married8==.;
replace married8=married3 if dead8==1 & married8==.;
replace married8=married2 if dead8==1 & married8==.;
replace married8=married1 if dead8==1 & married8==.;

replace married9=married8 if dead9==1 & married9==.;
replace married9=married7 if dead9==1 & married9==.;
replace married9=married6 if dead9==1 & married9==.;
replace married9=married5 if dead9==1 & married9==.;
replace married9=married4 if dead9==1 & married9==.;
replace married9=married3 if dead9==1 & married9==.;
replace married9=married2 if dead9==1 & married9==.;
replace married9=married1 if dead9==1 & married9==.;

replace married10=married9 if dead10==1 & married10==.;
replace married10=married8 if dead10==1 & married10==.;
replace married10=married7 if dead10==1 & married10==.;
replace married10=married6 if dead10==1 & married10==.;
replace married10=married5 if dead10==1 & married10==.;
replace married10=married4 if dead10==1 & married10==.;
replace married10=married3 if dead10==1 & married10==.;
replace married10=married2 if dead10==1 & married10==.;
replace married10=married1 if dead10==1 & married10==.;

replace married11=married10 if dead11==1 & married11==.;
replace married11=married9 if dead11==1 & married11==.;
replace married11=married8 if dead11==1 & married11==.;
replace married11=married7 if dead11==1 & married11==.;
replace married11=married6 if dead11==1 & married11==.;
replace married11=married5 if dead11==1 & married11==.;
replace married11=married4 if dead11==1 & married11==.;
replace married11=married3 if dead11==1 & married11==.;
replace married11=married2 if dead11==1 & married11==.;
replace married11=married1 if dead11==1 & married11==.;

replace married12=married11 if dead12==1 & married12==.;
replace married12=married10 if dead12==1 & married12==.;
replace married12=married9 if dead12==1 & married12==.;
replace married12=married8 if dead12==1 & married12==.;
replace married12=married7 if dead12==1 & married12==.;
replace married12=married6 if dead12==1 & married12==.;
replace married12=married5 if dead12==1 & married12==.;
replace married12=married4 if dead12==1 & married12==.;
replace married12=married3 if dead12==1 & married12==.;
replace married12=married2 if dead12==1 & married12==.;
replace married12=married1 if dead12==1 & married12==.;

replace married13=married12 if dead13==1 & married13==.;
replace married13=married11 if dead13==1 & married13==.;
replace married13=married10 if dead13==1 & married13==.;
replace married13=married9 if dead13==1 & married13==.;
replace married13=married8 if dead13==1 & married13==.;
replace married13=married7 if dead13==1 & married13==.;
replace married13=married6 if dead13==1 & married13==.;
replace married13=married5 if dead13==1 & married13==.;
replace married13=married4 if dead13==1 & married13==.;
replace married13=married3 if dead13==1 & married13==.;
replace married13=married2 if dead13==1 & married13==.;
replace married13=married1 if dead13==1 & married13==.;

replace married14=married13 if dead14==1 & married14==.;
replace married14=married12 if dead14==1 & married14==.;
replace married14=married11 if dead14==1 & married14==.;
replace married14=married10 if dead14==1 & married14==.;
replace married14=married9 if dead14==1 & married14==.;
replace married14=married8 if dead14==1 & married14==.;
replace married14=married7 if dead14==1 & married14==.;
replace married14=married6 if dead14==1 & married14==.;
replace married14=married5 if dead14==1 & married14==.;
replace married14=married4 if dead14==1 & married14==.;
replace married14=married3 if dead14==1 & married14==.;
replace married14=married2 if dead14==1 & married14==.;
replace married14=married1 if dead14==1 & married14==.;

if $INCLUDE2020 {;
replace married15=married14 if dead15==1 & married15==.;
replace married15=married13 if dead15==1 & married15==.;
replace married15=married12 if dead15==1 & married15==.;
replace married15=married11 if dead15==1 & married15==.;
replace married15=married10 if dead15==1 & married15==.;
replace married15=married9 if dead15==1 & married15==.;
replace married15=married8 if dead15==1 & married15==.;
replace married15=married7 if dead15==1 & married15==.;
replace married15=married6 if dead15==1 & married15==.;
replace married15=married5 if dead15==1 & married15==.;
replace married15=married4 if dead15==1 & married15==.;
replace married15=married3 if dead15==1 & married15==.;
replace married15=married2 if dead15==1 & married15==.;
replace married15=married1 if dead15==1 & married15==.;
};
sum sub*;

* make subXwX = sub(X-1)w(X-1) if the repondent died;
/* this code i superceded by revisions to mortal.do
sort HHID PN;
replace sub2w2=0 if dead2==1;
replace sub3w3=sub2w2 if dead3==1;
replace sub3w3=0 if sub2w2==. & dead3==1;
replace sub4w4=sub3w3 if dead4==1; 
replace sub4w4=sub2w2 if sub3w3==. & dead4==1;
replace sub5w5=sub4w4 if dead5==1; 
replace sub5w5=sub3w3 if sub4w4==. & dead5==1;
replace sub5w5=sub2w2 if sub5w5==. & dead5==1; 
replace sub6w6=sub5w5 if dead6==1;
replace sub6w6=sub4w4 if sub5w5==. & dead6==1;
sum sub*;
*/


sort HHID PN;
summ tenure* job* switch* socc* reason* ;
saveold `saved'merge2, replace version(13);
cap log close;
